home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS01.ADF / C / sparks.c < prev   
C/C++ Source or Header  |  1986-01-09  |  7KB  |  278 lines

  1.  
  2. /************************************************************************
  3.  * Sparks.c -- One of those graphics dazzlers which are so easy to do on
  4.  *            the Amiga.  Aside from that, I think it is a fairly decent
  5.  *            example of how to do simple things - menus, for example,
  6.  *            using Intuition.  Hope ya'all enjoy it.
  7.  *
  8.  *            Copyright (c) 1985 by Scott Ballantyne
  9.  *            Mainly so I can say give it away, distribute it, post it
  10.  *            on other nets, etc.
  11.  ************************************************************************/
  12.  
  13. #include <exec/types.h> /* Not all of these are used -- I include them */
  14. #include <exec/nodes.h> /* 'cause I'm sick of compiler warnings.       */
  15. #include <exec/lists.h>
  16. #include <exec/exec.h>
  17. #include <exec/execbase.h>
  18. #include <exec/ports.h>
  19. #include <exec/devices.h>
  20. #include <exec/memory.h>
  21. #include <hardware/blit.h>
  22. #include <graphics/copper.h>
  23. #include <graphics/regions.h>
  24. #include <graphics/rastport.h>
  25. #include <graphics/gfxbase.h>
  26. #include <graphics/gfxmacros.h>
  27. #include <graphics/gels.h>
  28. #include <intuition/intuition.h>
  29.  
  30. struct IntuitionBase *IntuitionBase = NULL;
  31. struct GfxBase *GfxBase = NULL;
  32.  
  33.  
  34. #define MAXX   640
  35.  
  36. struct NewScreen MyScreen =
  37. { 0,0,MAXX,200,4,0,1,HIRES, CUSTOMSCREEN, NULL, "Sparks!", 0,0,};
  38.  
  39.    struct NewWindow DrawWindow = {
  40.       0,0,MAXX,200,
  41.       0,1,
  42.       MENUPICK,
  43.       BORDERLESS | BACKDROP | ACTIVATE,
  44.       NULL,
  45.       NULL,
  46.       NULL,
  47.       NULL,
  48.       NULL,
  49.       0,0,0,0,
  50.       CUSTOMSCREEN,
  51.    };
  52.  
  53. struct Screen *Screen = NULL;
  54. struct Window *Backdrop = NULL;
  55. struct RastPort *DrawRP;
  56. struct ViewPort *DrawVP;
  57. struct IntuiMessage  *message;
  58.  
  59. struct MenuItem OnlyMenuItems[3];
  60. struct IntuiText OnlyMenuText[3];
  61. struct Menu OnlyMenu[1];
  62.  
  63. #define MAXLINES  125
  64. #define ERASE     0
  65. main()
  66. {
  67.    int lx1[MAXLINES], lx2[MAXLINES], x1, x2;
  68.    UBYTE ly1[MAXLINES], ly2[MAXLINES], y1, y2;
  69.    UBYTE  cl;
  70.    BYTE color;
  71.    int deltax1, deltay1, deltax2, deltay2;
  72.    ULONG class;
  73.    USHORT code, ItemNum;
  74.  
  75.    for(x1 = 0; x1 < MAXLINES; x1++)
  76.       lx1[x1] = ly1[x1] = lx2[x1] = ly2[x1] = 0;
  77.  
  78.    color = 2;
  79.  
  80.    x1 = x2 = 160;
  81.    y1 = y2 = 100;
  82.    cl = 0;
  83.    selectdelta(&deltax1, &deltay1, &deltax2, &deltay2);
  84.  
  85.    if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  86.          exit(1);
  87.  
  88.    if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0))) {
  89.       cleanitup();
  90.       exit(2);
  91.    }
  92.  
  93.    if(!(Screen = (struct Screen *)OpenScreen(&MyScreen))) {
  94.       cleanitup();
  95.       exit(3);
  96.    }
  97.  
  98.    DrawWindow.Screen = Screen;
  99.  
  100.    if(!(Backdrop = (struct Window *)OpenWindow(&DrawWindow))) {
  101.       cleanitup();
  102.       exit(4);
  103.    }
  104.  
  105.  
  106.    DrawRP = Backdrop->RPort;     /* Draw into backdrop window */
  107.    DrawVP = &Screen->ViewPort;   /* Set colors in Screens VP  */
  108.    setcolors();
  109.  
  110.    initmenuitems();
  111.    initmenu();
  112.    SetMenuStrip(Backdrop, &OnlyMenu[0]);
  113.  
  114.    FOREVER {
  115.       while(message = (struct IntuiMessage *)GetMsg(Backdrop->UserPort)) {
  116.          class = message->Class;
  117.          code = message->Code;
  118.          ReplyMsg(message);
  119.  
  120.          if (class == MENUPICK && code != MENUNULL) {
  121.             ItemNum = ITEMNUM( code );
  122.             switch (ItemNum) {
  123.                case 0:
  124.                   ShowTitle(Screen, FALSE);
  125.                   break;
  126.                case 1:
  127.                   ShowTitle(Screen, TRUE);
  128.                   break;
  129.                case 2:
  130.                   ClearMenuStrip(Backdrop);
  131.                   cleanitup();
  132.                   exit(0);
  133.             }
  134.          }
  135.       }
  136.  
  137.       draw(lx1[cl], ly1[cl], lx2[cl], ly2[cl], ERASE);
  138.       if (RangeRand(6) == 0)
  139.          color = RangeRand(16);
  140.       if (RangeRand(6) == 0)
  141.          selectdelta(&deltax1, &deltay1, &deltax2, &deltay2);
  142.       adjust_x(&x1, &deltax1);
  143.       adjust_y(&y1, &deltay1);
  144.       adjust_x(&x2, &deltax2);
  145.       adjust_y(&y2, &deltay2);
  146.       draw(x1, y1, x2, y2, color);
  147.       lx1[cl] = x1;
  148.       lx2[cl] = x2;
  149.       ly1[cl] = y1;
  150.       ly2[cl] = y2;
  151.       if (++cl >= MAXLINES)
  152.          cl = 0;
  153.    }
  154. }
  155.  
  156. cleanitup()    /* release allocated resources */
  157. {
  158.    if (Backdrop)
  159.       CloseWindow(Backdrop);
  160.    if (Screen)
  161.       CloseScreen(Screen);
  162.    if (GfxBase)
  163.       CloseLibrary(GfxBase);
  164.    if (IntuitionBase)
  165.       CloseLibrary(IntuitionBase);
  166. }
  167.  
  168. adjust_x(x, deltax)  /* make new xcor - if out of range adjust */
  169. int *x, *deltax;
  170. {
  171.    int junk;
  172.  
  173.    junk = *x + *deltax;
  174.    if (junk < 1 | junk >= MAXX) {
  175.       junk = *x;
  176.       *deltax *= -1;
  177.    }
  178.    *x = junk;
  179. }
  180.  
  181. adjust_y(y, deltay)
  182. UBYTE  *y;
  183. int  *deltay;
  184. {
  185.    int junk;
  186.  
  187.    junk = *y + *deltay;
  188.    if (junk < 1 | junk > 199) {
  189.       junk = *y;
  190.       *deltay *= -1;
  191.    }
  192.    *y = junk;
  193. }
  194.  
  195. selectdelta(dx1, dy1, dx2, dy2)
  196. int *dx1, *dy1, *dx2, *dy2;
  197. {
  198.    *dx1 = 2 * (RangeRand(7) - 3);
  199.    *dy1 = 2 * (RangeRand(7) - 3);
  200.    *dx2 = 2 * (RangeRand(7) - 3);
  201.    *dy2 = 2 * (RangeRand(7) - 3);
  202. }
  203.  
  204. draw(x1, y1, x2, y2, color)
  205. int x1, x2;
  206. UBYTE y1, y2;
  207. BYTE color;
  208. {
  209.    SetAPen(DrawRP, color);
  210.    SetDrMd(DrawRP, JAM1);
  211.    Move(DrawRP, x1, y1);
  212.    Draw(DrawRP, x2, y2);
  213. }
  214.  
  215. setcolors()
  216. {
  217.    SetRGB4(DrawVP, 0, 0, 0, 0);
  218.    SetRGB4(DrawVP, 1, 3, 5, 10);
  219.    SetRGB4(DrawVP, 2, 15, 15, 15);
  220.    SetRGB4(DrawVP, 3, 15, 6, 0);
  221.    SetRGB4(DrawVP, 4, 14, 3, 0);
  222.    SetRGB4(DrawVP, 5, 15, 11, 0);
  223.    SetRGB4(DrawVP, 6, 15, 15, 2);
  224.    SetRGB4(DrawVP, 7, 11, 15, 0);
  225.    SetRGB4(DrawVP, 8, 5, 13, 0);
  226.    SetRGB4(DrawVP, 9, 0, 0, 15);
  227.    SetRGB4(DrawVP, 10, 3, 6, 15);
  228.    SetRGB4(DrawVP, 11, 7, 7, 15);
  229.    SetRGB4(DrawVP, 12, 12, 0, 14);
  230.    SetRGB4(DrawVP, 13, 15, 2, 14);
  231.    SetRGB4(DrawVP, 15, 13, 11, 8);
  232. }
  233.  
  234. initmenuitems()
  235. {
  236.    short n;
  237.  
  238.    for(n = 0; n < 3; n++) {  /* One struct for each item */
  239.       OnlyMenuItems[n].NextItem = &OnlyMenuItems[n + 1]; /* next item */
  240.       OnlyMenuItems[n].LeftEdge = 0;
  241.       OnlyMenuItems[n].TopEdge = 10 * n;
  242.       OnlyMenuItems[n].Width = 112;
  243.       OnlyMenuItems[n].Height = 10;
  244.       OnlyMenuItems[n].Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
  245.       OnlyMenuItems[n].MutualExclude = 0;
  246.       OnlyMenuItems[n].ItemFill = (APTR)&OnlyMenuText[n];
  247.       OnlyMenuItems[n].SelectFill = NULL;
  248.       OnlyMenuItems[n].Command = 0;
  249.       OnlyMenuItems[n].SubItem = NULL;
  250.       OnlyMenuItems[n].NextSelect = 0;
  251.  
  252.       OnlyMenuText[n].FrontPen = 0;
  253.       OnlyMenuText[n].BackPen = 1;
  254.       OnlyMenuText[n].DrawMode = JAM2;
  255.       OnlyMenuText[n].LeftEdge = 0;
  256.       OnlyMenuText[n].TopEdge = 1;
  257.       OnlyMenuText[n].ITextFont = NULL;
  258.       OnlyMenuText[n].NextText = NULL;
  259.    }
  260.    OnlyMenuItems[2].NextItem = NULL; /* Last item */
  261.  
  262.    OnlyMenuText[0].IText = (UBYTE *)"Hide Title Bar";
  263.    OnlyMenuText[1].IText = (UBYTE *)"Show Title Bar";
  264.    OnlyMenuText[2].IText = (UBYTE *)"QUIT!";
  265. }
  266. initmenu()
  267. {
  268.    OnlyMenu[0].NextMenu = NULL;                 /* No more menus */
  269.    OnlyMenu[0].LeftEdge = 0;
  270.    OnlyMenu[0].TopEdge = 0;
  271.    OnlyMenu[0].Width = 85;
  272.    OnlyMenu[0].Height = 10;
  273.    OnlyMenu[0].Flags = MENUENABLED;             /* All items selectable */
  274.    OnlyMenu[0].MenuName = "Actions";
  275.    OnlyMenu[0].FirstItem = &OnlyMenuItems[0];   /* Pointer to first item */
  276. }
  277.  
  278.